//
//  ArtistsDataSource.m
//  MantuPlay
//
//  Created by Markus Sintonen on 14.8.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ArtistsDataSource.h"
#import "TrackDatabase.h"
#import "AlbumsDataSource.h"
#import "Artist.h"
#import "NSArrayExtensions.h"

#define ROW_HEIGHT 46.0
#define OBJECTCOUNTLABEL_TAG 4

@implementation ArtistsDataSource

@synthesize navigationBarTitle, artistsFromGenre;

- (id)init {
	if ((self = [super init])) {
		self.navigationBarTitle = @"Artists";
		
		showAllAlbumsCell = NO;
	}
	return self;
}

-(id)initWithArtistsFromGenre:(NSString*)genre {
	if ((self = [super init])) {
		if(genre) artistsFromGenre = [[NSString alloc] initWithString:genre];
		
		self.navigationBarTitle = @"Artists";
		
		showAllAlbumsCell = YES;
	}
	return self;
}

-(NSArray*)databaseObjectArray {
	return artists;
}

- (NSString*)name {
	return @"Artists";
}

- (NSString*)navigationBarName {
	return navigationBarTitle;
}

-(CGFloat)rowHeight {
	return ROW_HEIGHT;
}

-(BOOL)showSectioned {
	return YES;
}

-(BOOL)extraRowAtTop {
	return (showAllAlbumsCell && ([artists count] > 1));
}
-(BOOL)extraRowAtBottom {
	return ([artists count] > 5);
}

-(void)loadData {
	if(!artists) {
		NSArray *tempArtists = [[TrackDatabase sharedTrackDatabase] artistsFromGenre:artistsFromGenre];
		artists = [[NSArray alloc] initWithArray:tempArtists];
	}
}
-(void)unloadData {
	[artists release];
	artists = nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForObject:(id)obj atIndexPath:(NSIndexPath*)indexPath {
	static NSString *CellIdentifier = @"ArtistCell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:CellIdentifier] autorelease];
	}

	Artist *artist = (Artist*)obj;
	cell.text = artist.title;
	
	return cell;	
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForExtraRowAtTopIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllAlbumsCell"];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"AllAlbumsCell"] autorelease];
	}
	cell.text = @"All Albums";
	
	return cell;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForExtraRowAtBottomIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ObjectCountCell"];
	UILabel *albumCountCell;
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"ObjectCountCell"] autorelease];
		
		albumCountCell = [[[UILabel alloc] initWithFrame:CGRectMake(1.0, 1.0, 320.0-1.0, ROW_HEIGHT-2.0)] autorelease];
		albumCountCell.tag = OBJECTCOUNTLABEL_TAG;
		albumCountCell.textColor = [UIColor grayColor];
		albumCountCell.textAlignment = UITextAlignmentCenter;
		[cell.contentView addSubview:albumCountCell];
	} else {
		albumCountCell = (UILabel *)[cell.contentView viewWithTag:OBJECTCOUNTLABEL_TAG];
	}
	albumCountCell.text = [NSString stringWithFormat:@"%d artists", [artists count]];
	
	return cell;
}

-(id<DatabaseObjectDataSource>)tableView:(UITableView *)tableView dataSourceForSelectedObject:(id)obj atIndexPath:(NSIndexPath *)indexPath {
	Artist *artist = (Artist*)obj;
	AlbumsDataSource<DatabaseObjectDataSource> *albumsDataSource;
	albumsDataSource= [[AlbumsDataSource alloc] initWithAlbumsFromArtist:artist.title genre:artistsFromGenre];

	albumsDataSource.navigationBarTitle = artist.title;
	
	return [albumsDataSource autorelease];
}
-(id<DatabaseObjectDataSource>)tableView:(UITableView *)tableView dataSourceForSelectedExtraRowAtTopIndexPath:(NSIndexPath*)indexPath {
	AlbumsDataSource<DatabaseObjectDataSource> *albumsDataSource;
	albumsDataSource= [[AlbumsDataSource alloc] initWithAlbumsFromArtist:nil genre:artistsFromGenre];
	
	albumsDataSource.albumsFromGenre = self.artistsFromGenre;
	albumsDataSource.navigationBarTitle = @"Albums";
	
	return [albumsDataSource autorelease];
}

- (void)dealloc {
	[artists release];
	[navigationBarTitle release];
	[super dealloc];
}

@end
